package com.bocai.constant;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by jyf on 18/4/27.
 */
public class LoginFilter implements javax.servlet.Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) servletRequest;
        HttpServletResponse resp = (HttpServletResponse) servletResponse;
        // 获得当前请求的URI
        String nowPath = req.getRequestURI();
        if (nowPath.indexOf("/bocai-manager/c") > -1) {
            // 代表C端用户访问路径
            filterChain.doFilter(servletRequest, servletResponse);
        } else {
            // 代表S端用户访问路径
            if (req.getSession().getAttribute("systemUserId") == null) {
                if ("/bocai-manager/s/login.do".equals(nowPath) || "/bocai-manager/user//signBackstage".equals(nowPath)) {
                    filterChain.doFilter(req, resp);
                    return;
                } else {
                    resp.sendRedirect(req.getContextPath() + "/s/login.do");
                    return;
                }
            }
            filterChain.doFilter(servletRequest, servletResponse);
        }
    }

    @Override
    public void destroy() {

    }
}
